// 网关配置的前缀
var companyServer = "/companySer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量
var search_value = "";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    let accountStatus = $("#accountStatus").html();
    if(accountStatus !== "正常"){
        $("#forbidden").removeAttr("hidden");
    }

    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "memberOrder");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    getOrderInfo(currentPage, pageSize, search_value);
}

function getOrderInfo(currentPage, pageSize, search_value) {
    $.ajax({
        type: "GET",
        async: false,
        url: companyServer + "/company/orderInfo",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value
        },
        success: function (data) {
            console.log("所有的订单数据 - 第 " + currentPage + " 页");
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#orderList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                $("#orderList").append(
                    "<tr class='job-application-item'>" +

                    "<td class='application-employer'>" +
                    "<a class='dotted'>" + item.orderId + "</a>" +
                    "</td>" +

                    "<td class='application-job'>" +
                    "<h3>" +
                    "<a onclick='companyDetail(\"" + item.companyId + "\")'>" + item.companyName + "</a>" +
                    "</h3>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span> " + item.memberLevel + " </span>" +
                    "</td>" +


                    "<td class='application-created'>" +
                    "<span> " + item.createDate + " </span>" +
                    "</td>" +

                    "<td class='application-created'>" +
                    "<span> " + item.endDate + " </span>" +
                    "</td>" +
                    "</tr>"
                )

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getOrderInfo(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

//  搜索框
function click_search() {
    var tmp = $('#search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;

    getOrderInfo(currentPage, pageSize, search_value);
}

function companyDetail(companyId) {
    location.href = "/oa/company/companyDetailsPage/" + companyId;
}
